/*
 * Rotate Image
You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?
 */
package com.xinpan.exercise;

public class RotateImage {
    public void rotate(int[][] matrix) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int n = matrix.length;
        int tmp = 0;
        
        for(int i = 0; i < n/2; i++)
        {
            for(int j = i; j < n-i-1; j++)
            {
                int x1 = j;
                int y1 = n-i-1;
                int tmp1 = matrix[x1][y1];
                matrix[x1][y1] = matrix[i][j];
                
                int x2 = n-i-1;
                int y2 = n-j-1;
                int tmp2 = matrix[x2][y2];
                matrix[x2][y2] = tmp1;
                
                int x3 = n-j-1;
                int y3 = i;
                int tmp3 = matrix[x3][y3];
                matrix[x3][y3] = tmp2;
                
                matrix[i][j] = tmp3;
            }
        }
    }
    
    public static void main(String[] args)
    {
    	 RotateImage ri = new  RotateImage();
    	 int[][] m = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
    	 ri.rotate(m);
    	 System.out.print("1");
    }
}
